--################# HEADER #################
AddCSLuaFile("cl_init.lua");
AddCSLuaFile("shared.lua");
include("shared.lua");

--################# SENT CODE ###############

--################# Init @meeces2911
function ENT:Initialize()
	--Make us like a LS3 node
	--self.BaseClass.Initialize(self)
	--self:SetNetworkedInt( "range", 2048 )
	--self:SetNetworkedInt("overlaymode",1)
	self.Entity:SetModel("models/mak/gen/gen.mdl");
	self.Entity:PhysicsInit(SOLID_VPHYSICS);
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS);
	self.Entity:SetSolid(SOLID_VPHYSICS);
	self.Entity:SetUseType(SIMPLE_USE);
	self.Phys = self.Entity:GetPhysicsObject();
	if(self.Phys:IsValid()) then
		self.Phys:Wake();
		self.Phys:SetMass(10);
	end
	self:AddResource("energy",0); --register as LS device but no storage
	self.items = {}
	self.modifier = StarGate.CFG:Get("ZPM_Recharger","modifier",8);
	self.moreenergy = false;
end

--################# Think @meeces2911
function ENT:Think()	
	--Borrowed from the LS3 resource node, to use the ents connected to it
	
	if self.items ~= nil then
		for _,ent in pairs(self.items) do
			if ent and ValidEntity(ent) then
				if ((ent:GetClass():lower() == "zero_point_module") or (ent:GetClass():lower() == "zero_point_module_mk2")) then
					ent.depleted = false;
					self:recharge(ent,"ZPE");
				end
				if ent:GetClass():lower() == "naquadah_bottle" then
					ent.depleted = false;
					self:recharge(ent,"NE");
				end
			end
		end
	else
		self:SupplyResource("ZPE",0);
		self:SupplyResource("NE",0);
	end
	
	self:ShowOutput()
	
	self.Entity:NextThink(CurTime()+0.5);
	return true;
end

function ENT:recharge(ent,restype)
	if not ent:IsValid() then return end;
	if (restype == nil) then return end;	
	
	--sanity check
	if self.modifier < 2 then self.modifier = 8; end
	
	--[[1% every 3 sec... and 1 sec = 2 thinks, so... 6 thinks = 3 sec ?
		so that means every think we want to do 1/6% of charge.
		A zpm has: 10,000,000 ZPE units (10m/100 (100%) /6 (1/6)
	]]--
	local time = math.Clamp(StarGate.CFG:Get("ZPM_Recharger","time",3),1,100);
	local res = math.floor((ent:GetUnitCapacity(restype)/100/(2*time))*self.modifier);
	
	if self:GetResource("energy") > res then
		self.moreenergy = false;
		self:ConsumeResource("energy",res);
		ent:SupplyResource(restype,res);
	else
		self.moreenergy = true;
	end
end

function ENT:ShowOutput()
	local num = 0;
	if self.items ~= nil then
		num = #self.items;
	end
	if not self.moreenergy then
		self:SetOverlayText("Devices Connected: "..num.."\n Charging...");
	else
		self:SetOverlayText("Devices Connected: "..num.."\n More Energy Required!");
	end
	if num == 0 then
		self:SetOverlayText("Devices Connected: 0");
	end
end

function ENT:Use()
	self.items = {};
	local field = math.Clamp(StarGate.CFG:Get("ZPM_Recharger","Recharge_field",2048), 50, 2048);
	for _,v in pairs(ents.FindInSphere(self.Entity:GetPos(),field)) do
		if v:GetClass():lower() == "zero_point_module" then
			table.insert(self.items,v);
			local edata = EffectData()
 			edata:SetEntity( self.Entity )
 			util.Effect( "propspawn", edata, true, true )
			edata:SetEntity( v )
 			util.Effect( "propspawn", edata, true, true )
		end
		if v:GetClass():lower() == "zero_point_module_mk2" then
			table.insert(self.items,v);
			local edata = EffectData()
 			edata:SetEntity( self.Entity )
 			util.Effect( "propspawn", edata, true, true )
			edata:SetEntity( v )
 			util.Effect( "propspawn", edata, true, true )
		end
		if v:GetClass():lower() == "naquadah_bottle" then
			table.insert(self.items,v);
			local edata = EffectData()
 			edata:SetEntity( self.Entity )
 			util.Effect( "propspawn", edata, true, true )
			edata:SetEntity( v )
 			util.Effect( "propspawn", edata, true, true )
		end
	end
end